package com.edp.wms.basedata.controller;

import com.edp.wms.basedata.domain.GoodsCategory;
import com.edp.wms.basedata.service.GoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类管理
 *
 * @author ruoyi
 * @date 2020-05-07
 */
@RestController
@RequestMapping("/basedata/goodsCategory")
@RequiredArgsConstructor
@Api(tags = "商品分类管理")
public class GoodsCategoryController {

    private final GoodsCategoryService goodsCategoryService;

    /**
     * 获取所有货品分类列表
     */
    @GetMapping
    @ApiOperation("获取货品分类列表")
    public ResponseEntity<List<GoodsCategory>> getAllCategories() {
        List<GoodsCategory> categories = goodsCategoryService.list();
        return ResponseEntity.ok(categories);
    }

    /**
     * 根据ID获取货品分类详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取货品分类详情")
    public ResponseEntity<GoodsCategory> getCategoryById(@PathVariable Long id) {
        GoodsCategory category = goodsCategoryService.getById(id);
        if (category != null) {
            return ResponseEntity.ok(category);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 创建新货品分类
     */
    @PostMapping
    @ApiOperation("创建货品分类")
    public ResponseEntity<GoodsCategory> createCategory(@RequestBody GoodsCategory category) {
        // 检查必要字段
        if (category.getCategoryCode() == null || category.getCategoryName() == null) {
            return ResponseEntity.badRequest().build();
        }

        // 设置默认状态
        if (category.getStatus() == null) {
            category.setStatus("0");
        }

        boolean saved = goodsCategoryService.save(category);
        if (saved) {
            GoodsCategory savedCategory = goodsCategoryService.getById(category.getId());
            return ResponseEntity.status(HttpStatus.CREATED).body(savedCategory);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 更新货品分类信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新货品分类信息")
    public ResponseEntity<GoodsCategory> updateCategory(@PathVariable Long id, @RequestBody GoodsCategory category) {
        // 检查要更新的分类是否存在
        GoodsCategory existingCategory = goodsCategoryService.getById(id);
        if (existingCategory == null) {
            return ResponseEntity.notFound().build();
        }

        // 设置ID确保更新正确的记录
        category.setId(id);

        // 执行更新操作
        boolean updated = goodsCategoryService.updateById(category);
        if (updated) {
            // 返回更新后的完整对象
            GoodsCategory updatedCategory = goodsCategoryService.getById(id);
            return ResponseEntity.ok(updatedCategory);
        } else {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 删除货品分类
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除货品分类")
    public ResponseEntity<Void> deleteCategory(@PathVariable Long id) {
        boolean deleted = goodsCategoryService.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }


}
